<?php
/**
 * 矿机
 */
namespace app\api\controller;
use app\api\model\MemberMining as MemberMiningModel;
use app\api\model\Mining as MiningModel;
use app\common\model\Banner as BannerModel;
use app\common\common;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\facade\Db;
use app\api\model\MiningSpecification as MiningSpecificationModel;

class Mining extends Baek
{
    /**
     * 矿机首页
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function index(){
        $data = [];
        //矿机banner
        $data['banner']    = BannerModel::position_list('mining',$this->language);
        return api_success($data);
    }

    /**
     * 获取矿机列表
     * @return \think\Response
     * @throws \think\db\exception\DbException
     */
    function getList(){
        $data = MiningSpecificationModel::apiGetListAll($this->language);

        foreach ($data as &$v){
            $getDetails  = MiningModel::getDetails($v['pid']);
            $v['currency']  = $getDetails['currency'];
            $v['equipment']  = $getDetails['equipment'];
            $v['chip']  = $getDetails['chip'];
            $v['total_num']  = $getDetails['total_num'];
            $v['possess'] = MemberMiningModel::countMining($this->mid,$v['id'],['status'=>1]);
        }

        return api_success($data);
    }

    /**
     * 购买矿机
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function pay_mining(){
        $id  = $this->request->post('id', '', 'intval');  //矿机ID
        $num  = $this->request->post('num', '', 'intval');  //购买数量

        if(empty($id) && !is_int($num) && $num <= 0) return api_error('','The purchase quantity cannot be less than one',$this->language);

        //查询当前矿机详情
        $mining = MiningSpecificationModel::getDetails($id);
        //判断是否有该矿机
        if(empty($mining)) return api_error('','Parameter error',$this->language);
        //判断矿机是否可购买
        if($mining['status'] != 1) return api_error('','Mining machine cannot be purchased',$this->language);

        //判断矿机个人最大持有数量
        if($mining['max_possess'] != 0){
            //查询该矿机我拥有的数量
            $possess = MemberMiningModel::countMining($this->mid,$id,['status'=>1]);
            $possess = $possess+$num;
            if($mining['max_possess'] < $possess) return api_error('','Exceed the maximum purchase quantity',$this->language);
        }
        //计算支付金额
        $money = bcmul($num,$mining['money'],2);

        Db::startTrans();
        try {
            //购买矿机
            $member_mining_id = MemberMiningModel::payMining($this->mid,$mining,$num,$money);

            if(!$member_mining_id){   //判断是否购买成功
                Db::rollback();
                return api_error('','Purchase failed',$this->language);
            }
            //扣除余额
            $currency = common::key_currency($mining['currency']);
            $pay_status = UserMoneyLogModel::change($this->mid,$currency,-$money,'pay_mining',$member_mining_id,'购买矿机，支付：'.$money);
            if($pay_status != true){  //支付失败
                Db::rollback();
                return api_error($pay_status,'Purchase failed',$this->language);
            }

            // 提交事务
            Db::commit();
            return api_success($id);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return api_error('','Purchase failed',$this->language);
        }

    }

    /**
     * 我的矿机
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function myList(){
        $page  = $this->request->post('page', 1, 'intval');
        $limit  = $this->request->post('limit', 20, 'intval');

        $data = MemberMiningModel::getMemberList($this->mid,$page,$limit,'id desc',$this->language);

        $data['profit'] = 0;

        return api_success($data);
    }

}